package com.oocl.chatroom.client.ui;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;

import com.oocl.chatroom.client.Client;
import com.oocl.chatroom.client.listener.ReceiverListener;
import com.oocl.chatroom.client.sender.ClientSender;
import com.oocl.chatroom.network.protocol.Message;

public class Register extends JFrame {
	
	private Client client;
	
	private ObjectOutputStream out;
	
	private boolean nflag = true;
	private boolean pflag = true;
	private boolean cflag = true;
	
	private JTextField nameField;
	private JPasswordField pwdField;
	private JPasswordField confirmPwdField;
	private JLabel lbStatus;

	public void hint(){
		if(nflag && (new String(nameField.getText()).length()==0)){
			nameField.setText("UserName");
			nameField.setForeground(Color.GRAY);
		}
		else if(nflag && new String(nameField.getText()).equals("UserName")){
			nameField.setText("");
			nameField.setForeground(Color.BLACK);
		}else{
			nameField.setText(nameField.getText());
			nameField.setForeground(Color.BLACK);
			
		}
	}
	public Register() {
		setResizable(false);
		setSize(457, 418);
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);

		nameField = new JTextField();
		nameField.setText("UserName");
		nameField.setForeground(Color.GRAY);
		nameField.setHorizontalAlignment(SwingConstants.CENTER);
		nameField.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				nflag=true;
				hint();
			}
			
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				nflag=false;
				hint();
			}
			
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		nameField.addFocusListener(new FocusListener() {
			
			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				nflag=true;
				hint();
			}
			
			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		nameField.setFont(new Font("Trebuchet MS", Font.PLAIN, 18));
		nameField.setBounds(98, 100, 252, 32);
		getContentPane().add(nameField);
		nameField.setColumns(10);

		
		pwdField = new JPasswordField() {
			public void paint(Graphics g) {
				super.paint(g);
				if (pflag && (new String(pwdField.getPassword()).length() == 0)) {
					g.setColor(Color.GRAY);
					g.drawString("Password", 90, 22);
				}
			}
		};
		pwdField.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub	
			}
			
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}
			
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				pflag = true;
				pwdField.repaint();
			}
			
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
			
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		pwdField.addFocusListener(new FocusListener() {

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				pflag = true;
				pwdField.repaint();
			}

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub
				pflag = false;
				pwdField.repaint();
			}
		});
		pwdField.setHorizontalAlignment(SwingConstants.CENTER);
		pwdField.setFont(new Font("Tahoma", Font.PLAIN, 18));
		pwdField.setBounds(98, 156, 252, 32);
		getContentPane().add(pwdField);
		pwdField.setColumns(10);

		
		confirmPwdField = new JPasswordField() {
			public void paint(Graphics g) {
				super.paint(g);
				if (cflag && (new String(confirmPwdField.getPassword()).length() == 0)) {
					g.setColor(Color.GRAY);
					g.drawString("Confirm Password", 60, 22);
				}
			}
		};
		confirmPwdField.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub			
			}
			
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}
			
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				cflag = true;
				pwdField.repaint();
			}
			
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
			
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		confirmPwdField.addFocusListener(new FocusListener() {

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				cflag = true;
				pwdField.repaint();
			}

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub
				cflag = false;
				pwdField.repaint();
			}
		});
		confirmPwdField.setHorizontalAlignment(SwingConstants.CENTER);
		confirmPwdField.setFont(new Font("Tahoma", Font.PLAIN, 18));
		confirmPwdField.setColumns(10);
		confirmPwdField.setBounds(98, 214, 252, 32);
		getContentPane().add(confirmPwdField);

		JButton regBtn = new JButton("Register");
		regBtn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("enter");
				try {
					if(checkPwd()){
						//Connect to entry server
						System.out.println("it's true");
						Socket socket=new Socket("127.0.0.1",8889);
						System.out.println("pwd same");
						String name=nameField.getText();
						String pwd=";"+pwdField.getText();
						Message msg=new Message(name+pwd,6,"","",System.currentTimeMillis());
						client=new Client(socket);
						//Send register msg to the server 
						send(msg);
						//Receive the msg from the server
						//Parse the msg
						if(new ReceiverListener(client).entryMsg()){
							lbStatus.setText("Register success.Goto login in 3s...");
							//this.wait(3000);
							Login l=new Login();
							l.setVisible(true);
							Register.this.dispose();//Close the current window		
						}else{
							lbStatus.setText("Register fail.");
						}
						//new Thread(new ReceiverListener())
					}else{
						lbStatus.setText("Make sure the passwords are same.");
					}
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		regBtn.setFont(new Font("Tahoma", Font.BOLD, 16));
		regBtn.setBounds(147, 302, 161, 32);
		getContentPane().add(regBtn);

		JLabel lblRegister = new JLabel("IM Register");
		lblRegister.setHorizontalAlignment(SwingConstants.CENTER);
		lblRegister.setFont(new Font("Tw Cen MT Condensed Extra Bold",
				Font.PLAIN, 25));
		lblRegister.setBounds(106, 30, 228, 47);
		getContentPane().add(lblRegister);
		
		lbStatus = new JLabel("");
		lbStatus.setForeground(new Color(204, 0, 51));
		lbStatus.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lbStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lbStatus.setBounds(59, 257, 329, 32);
		getContentPane().add(lbStatus);
	}
	
	
	public boolean checkPwd(){
		//System.out.println(pwdField.getPassword());
		if(pwdField.getText().equals(confirmPwdField.getText())){
			System.out.println(pwdField.getPassword());
			System.out.println(confirmPwdField.getPassword());
			return true;
		}else{
			System.out.println("hello");
			System.out.println(pwdField.getPassword());
			System.out.println(confirmPwdField.getPassword());
			System.out.println(pwdField.getText());
			return false;
		}
	}
	private void send(Message msg) throws IOException {
		ClientSender sender = new ClientSender(client.getOut());
		sender.send(msg);
	}
}
